mc.game.personal = function(){};
mc.game.personal.timeout = 1500;

mc.game.personal.show = function(){
	mc.game.army.show_for(mc.game.personal);
}

mc.game.personal.leader_click = function(event){
	var entry = mc.game.army.leader_select(this);
	var to = mc.cmd.get_right_inner(1).empty();
	
	mc.game.load_start();
	$.when(
		mc.game.personal.show_unit(entry,to)
	).then(mc.game.load_end);
	
	if(event){
		event.stopImmediatePropagation();
	}
}
mc.game.personal.show_unit = function(entry,to1,to2){
	var source = mc.leader_list.find_source();
	if(source.entry == entry){
		return $.when(
				mc.leader_list.ask_unit(entry)
			).then(function(){
				mc.game.personal.draw_source(entry,to1);
			});
	}
	return $.when(
			mc.leader_list.ask_unit(entry),
			mc.leader_list.ask_unit(source.entry)
		).then(function(){
			mc.game.personal.draw_unit(entry,to1,to2);
		});
}

mc.game.personal.draw_source = function(entry,to){
	var o = $.tmpl("personal_unit_source",mc.leader_list.personal(entry,true)).appendTo(to);
	var i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
}
mc.game.personal.draw_unit = function(entry,to){
	var o = $.tmpl("personal_unit_list",mc.leader_list.personal(entry)).appendTo(to);
	var i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
}
mc.game.personal.redraw_unit = function(entry){
	var to = mc.cmd.get_right_inner(1).empty();
	mc.game.personal.show_unit(entry,to);
}

mc.game.personal.unit_move = function(show,source,target,proto,count){
	mc.game.load_start();
	$.when(
		mc.request(
			"game",
			{
				source : source,
				target: target,
				proto: proto,
				count: count,
				cmd: "units.move"
			},
			function(data){
				mc.leader_list.set_units(this.source, data.source);
				mc.leader_list.set_units(this.target, data.target);
				mc.game.personal.redraw_unit(this.show);
			},
			{
				source : source,
				target: target,
				show: show
			}
		)
	).then(mc.game.load_end);
}
// add > 0 - to leader
// add < 0 - to source
mc.game.personal.set_delay = function(source,target,proto,add){
	var data = this.delayed;
	if(!data || data.proto != proto){
		data = {
			source: source,
			target: target,
			proto: proto,
			count: 0,
			tid: false
		};
	}
	if(data.tid){
		window.clearTimeout(data.tid);
		data.tid = false;
	}

	var t = $("#unit_list").tmplItem();
	var l = t.data.list;
	var real_add = 0;
	for(var i =0; i< l.length; i++){
		if(l[i].proto.entry == proto){
			if(add > 0){
				if(l[i].source > 0){
					l[i].count++;
					l[i].source--;
					real_add += add;
				}
			}else if(add<0){
				if(l[i].count > 0){
					l[i].count--;
					l[i].source++;
					real_add += add;
				}
			}
			break;
		}
	}
	data.count += real_add;
	data.tid = window.setTimeout(mc.game.personal.run_delay,mc.game.personal.timeout);
	this.delayed = data;
	if(real_add){
		t.update();
	}
}
mc.game.personal.run_delay = function(){
	var data = mc.game.personal.delayed;
	if(data.count > 0){
		mc.game.personal.unit_move(data.target,data.source,data.target,data.proto,data.count);
	}else{
		mc.game.personal.unit_move(data.target,data.target,data.source,data.proto,-data.count);
	}
	delete mc.game.personal.delayed;
}


mc.game.personal.toleader = function(source,target,proto){
	this.set_delay(source,target,proto,1);
}
mc.game.personal.tosource = function(source,target,proto){
	this.set_delay(source,target,proto,-1);
}
mc.game.personal.addsource = function(leader,proto){
	mc.request(
		"game",
		{
			leader: leader,
			proto: proto,
			cmd: "units.add"
		},
		function(data){
			mc.leader_list.set_units(this.leader, data);
			mc.game.personal.redraw_unit(this.leader);
		},
		{
			leader: leader
		}
	);
}
